
******
** Figure 5
******
set printcolor gs1, permanently
clear all
set more off
program drop _all
cap log close

///////////
/// Product-level Estimation
///////////

/// Sample used for estimation
use "INTERMEDIATE/product_RMS_def10_firm_sampleE_06_15.dta", clear
merge m:1 module using "INPUT/units_per_module_after_standardization.dta"
keep if _merge==3
keep year product quarter group module firm price revenue quantity cohort type censored  
drop if revenue==. | quantity==. | price==.
drop if firm==.
drop if group==.

/// Import elasticities
rename group product_group_code
merge m:1 product_group_code using "INPUT/Elasticities.dta", keep(match) keepusing(sigma eta omega) nogenerate
rename omega delta
rename product_group_code group

gen lnrevenue_uijt=ln(revenue)
gen lnprice_uijt=ln(price)
gen lnquantity_uijt=ln(quantity)

bys quarter group: egen M_jt = count(group)

* 1st components
bys group quarter: egen double rev_jt = sum(revenue)
gen double lnrev_jt=ln(rev_jt)
gen S_uijt_jt = revenue/rev_jt
gen lnS_uijt_jt=ln(S_uijt_jt)
bys quarter group: egen double lnS_uijt_jt_sum = sum(lnS_uijt_jt)
replace lnS_uijt_jt_sum = lnS_uijt_jt_sum/M_jt

* 2nd components
bys firm group quarter: egen double rev_ijt = sum(revenue)
gen S_ijt_jt=rev_ijt/rev_jt
gen lnS_ijt_jt=ln(S_ijt_jt)
bys quarter group: egen double lnS_ijt_jt_sum = sum(lnS_ijt_jt)
replace lnS_ijt_jt_sum = lnS_ijt_jt_sum/M_jt

* 3rd component
bys quarter group: egen double lnprice_uijt_sum = sum(lnprice_uijt)
replace lnprice_uijt_sum = lnprice_uijt_sum/M_jt

* Gamma
gen lngamma_uijt=(1/(sigma-1))*(lnS_uijt_jt-lnS_uijt_jt_sum)+((sigma-eta)/((1-eta)*(1-sigma)))*(lnS_ijt_jt-lnS_ijt_jt_sum)+(lnprice_uijt-lnprice_uijt_sum)
gen gamma_uijt=exp(lngamma_uijt)

* Get Scope
bys firm quarter group: egen double N_ijt = count(group)
gen double lnN_ijt=ln(N_ijt)

* Price index firm
gen double temp=(price/gamma_uijt)^(1-sigma)
bys firm quarter group: egen double P_ijt=sum(temp)
replace P_ijt=P_ijt^(1/(1-sigma))
gen double lnP_ijt=ln(P_ijt)
su group if P_ijt ==.
su P_ijt, d
drop temp

* Price index group
gen double temp=P_ijt^(1-eta) 
bys quarter group: egen double P_jt= sum(temp/N_ijt)
replace P_jt=P_jt^(1/(1-eta))
gen double lnP_jt=ln(P_jt)
su group if P_jt ==.
su P_jt, d
drop temp

* Get markups
gen double mu_ijt=(eta-(eta-1)*S_ijt)/(eta-(eta-1)*S_ijt-1)
su mu_ijt,d
gen double lnmu_ijt=ln(mu_ijt)

* Get z_uijt
gen double lnz_uijt=lnprice_uijt-lnmu_ijt
gen double z_uijt=exp(lnz_uijt)
su z_uijt, d

* Get a_uijt
gen double lna_uijt=lnz_uijt-ln(1+delta)-delta*lnquantity_uijt
gen double a_uijt=exp(lna_uijt)
su a_uijt, d

* Get Dispersion
gen double temp=(gamma_uijt/z_uijt)^(sigma-1)
bys firm quarter group: egen double sumratio=sum(temp)
gen double dispersion_uijt=(1/N_ijt)*(sumratio/temp)
gen double lndispersion_uijt=ln(dispersion_uijt)
drop temp sumratio

* To save
drop lnS_uijt_jt_sum lnS_ijt_jt_sum lnprice_uijt_sum
save "INTERMEDIATE/estimates_products.dta", replace


///////////
/// Firm-level Estimation
///////////

use "INTERMEDIATE/estimates_products.dta", clear

* Get geometric means
bys firm group quarter: egen lngamma_ijt=sum(lngamma_uijt)
replace lngamma_ijt=lngamma_ijt/N_ijt

bys firm group quarter: egen lnz_ijt=sum(lnz_uijt)
replace lnz_ijt=lnz_ijt/N_ijt

gen lngamma_lnz_ijt=lngamma_ijt-lnz_ijt

* Get Dispersion
gen double temp=(gamma_uijt/z_uijt)^(sigma-1)
gen double temp2=(exp(lngamma_lnz_ijt))^(sigma-1)
bys firm group quarter: egen double sumratio=sum(temp)
gen double dispersion_ijt=(1/N_ijt)*(sumratio/temp2)
gen double lndispersion_ijt=ln(dispersion_ijt)
drop temp temp2 sumratio

keep firm group quarter rev_ijt P_ijt lngamma_ijt lnz_ijt lngamma_lnz_ijt lnmu_ijt lnN_ijt lndispersion_ijt lnP_jt lnrev_jt eta sigma delta
duplicates drop
duplicates drop firm group quarter, force

* save
save "INTERMEDIATE/estimates_firms.dta", replace



///////////
/// Running Regressions and Drawing Figures
///////////

use "INTERMEDIATE/product_RMS_def10_firm_sampleE_06_15.dta", clear
merge m:1 module using "INTERMEDIATE/units_per_module_after_standardization.dta"
keep if _merge==3
drop _merge
merge 1:1 product quarter using "INTERMEDIATE/estimates_products.dta"
keep if _merge==3
drop _merge
merge m:1 firm group quarter using  "INTERMEDIATE/estimates_firms.dta", keep(1 3) keepusing(lndispersion_ijt lngamma_ijt lnz_ijt)
keep if _merge==3
drop _merge

*   New Variables
**************************
* Outcome variables

// extra variables
gen lngamma_cannibalization=lngamma_uijt-lngamma_ijt
gen lnz_cannibalization=lnz_uijt-lnz_ijt
gen ln_revenue=ln(revenue)
gen ln_price=ln(price)
gen ln_quantity=ln(quantity)

// components to use for decomposition
gen comp12=(eta-1)*(lngamma_uijt-lnz_uijt)			// appeal-to-cost:
gen comp1=(eta-1)*lngamma_uijt                        // appeal
gen comp2=-(eta-1)*lnz_uijt						      // cost

gen comp3=-(eta-1)*lnmu_ijt                            // markup

gen comp52=-((sigma-eta))*((lngamma_ijt-lnz_ijt)-(lngamma_uijt-lnz_uijt)) // appeal-to-cost cannibalization:
gen comp521=((sigma-eta))*lngamma_cannibalization    // appeal cannibalization
gen comp522=-((sigma-eta))*lnz_cannibalization       // cost cannibalization

gen comp4=-((sigma-eta)/(sigma-1))*lnN_ijt             // scope

gen comp51=-((sigma-eta)/(sigma-1))*lndispersion_ijt   // dispersion

// Other
gen firmappeal=(sigma-eta)*(lngamma_ijt-lnz_ijt)
gen comp5=-((sigma-eta)/(sigma-1))*lndispersion_uijt 
gen comp6=(eta-1)*ln(P_jt)+ln(rev_jt)
gen comp_total=comp1+comp2+comp3+comp4+comp5+comp6
gen check=ln(revenue)-comp_total
gen checkX=comp5-(comp51+comp52)

* Fixed-effects
egen fe=group(module quarter)
qui tab cohort, gen(cohort_d)

keep if cohort>=3 & cohort<=23 
drop if age>16 
drop if age==0
drop if maxAge<=16
keep if flag_longitudinal=="Complete"

foreach num of numlist 5/23 {  
gen cohort_nd`num'=cohort_d`num'-((`num'-1)*cohort_d4-(`num'-2)*cohort_d3)
}

foreach var of varlist ln_revenue comp12 comp3 comp4 comp51 comp52  { 

preserve
qui areg `var' i.age cohort_nd*, absorb(fe)
matrix results=[e(b)',vecdiag(e(V))']
svmat results
rename results1 coef
rename results2 se
drop cohort_nd* 

keep coef se
drop if coef == .
gen age=_n
drop if age>16
gen coef_u = coef + 1.96*sqrt(se)
gen coef_l = coef - 1.96*sqrt(se)

save "INTERMEDIATE/results_`var'.dta", replace
restore

}


* Drawing Figure 5

use "INTERMEDIATE/results_ln_revenue.dta", clear
rename coef* revenue_coef*
rename se revenue_se
merge 1:1 age using "INTERMEDIATE/results_comp12.dta"
drop _merge
rename coef* comp12_coef*
rename se comp12_se
merge 1:1 age using "INTERMEDIATE/results_comp3.dta"
drop _merge
rename coef* comp3_coef*
rename se comp3_se
merge 1:1 age using "INTERMEDIATE/results_comp4.dta"
drop _merge
rename coef* comp4_coef*
rename se comp4_se
merge 1:1 age using "INTERMEDIATE/results_comp51.dta"
drop _merge
rename coef* comp51_coef*
rename se comp51_se
merge 1:1 age using "INTERMEDIATE/results_comp52.dta"
drop _merge
rename coef* comp52_coef*
rename se comp52_se


twoway ///
(line revenue_coef age if age<=16, lp(solid) color(midblue) lwidth(thick)) ///
(line comp12_coef age if age<=16, lp(dash) color(navy) lwidth(thick)) ///
(line comp3_coef age if age<=16,  lp(dash) color(orange) lwidth(medthick)) ///
(line comp52_coef age if age<=16,  lp(shortdash) color(red*0.9) lwidth(thick)) ///
(line comp4_coef age if age<=16,  lp(longdash) color(green) lwidth(medthick)) ///
(line comp51_coef age if age<=16,  lp(longdash) color(brown) lwidth(medthick)) ///
, ytitle("Estimated (log scale)") xtitle("Age (quarters)") xlabel(4(4)16) ///
title("") ylabel(-0.8(0.4)0.4) ///
legend( row(3) order(1 "sales" 2 "appeal-to-cost" 3 "markup" 4 "appeal-to-cost cannibalization" 5 "size" 6 "dispersion")) ///
graphregion(color(white)) plotregion(fcolor(white)) legend(region(lcolor(white))) note("")
graph export "OUTPUT/Figure5.eps", replace
